﻿using BPMN.Application.IService;
using BPMN.Domain;
using BPMN.Domain.Entity.User;
using BPMN.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using BPMN.DomianShare;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using BPMN.Application.Contracts.DTO.Request.UserInfo;
using BPMN.Application.Contracts.DTO.Request.Role;
using BPMN.Application.Contracts.DTO.Response.Role;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.Contracts.DTO.Request.PerRole;
using BPMN.Application.Contracts.DTO.Response.RolePermission;
using BPMN.Infrastructure.Repository;
using System.Collections;

namespace BPMN.Application.Service
{
    public class RoleAppService : AppServiceBase<Role>, IRoleAppService
    {
        private readonly IRepositoryBase<Role> RepRole;
        private readonly IRepositoryBase<RolePermission> RepRolePermission;
        private readonly IMapper mapper;

        public IRepositoryBase<Extend> Extend { get; }
        public IRepositoryBase<Attributes> Attr { get; }

        public RoleAppService(IRepositoryBase<Role> RepRole, IMapper mapper, IRepositoryBase<RolePermission> RepRolePermission, IRepositoryBase<Extend> extend, IRepositoryBase<Attributes> attr) : base(RepRole)
        {
            this.RepRole = RepRole;
            this.mapper = mapper;
            this.RepRolePermission = RepRolePermission;
            Extend = extend;
            Attr = attr;
        }

        /// <summary>
        ///  角色添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddRole(RoleRequstDto entity)
        {
            var slist = RepRole.GetAll().Result.Where(x => x.Name == entity.Name && x.IsDel == true).FirstOrDefault();
            if (slist != null)
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "角色名称已存在");
            }

            var obj = mapper.Map<Role>(entity);
            int n = await RepRole.Add(obj);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }
        /// <summary>
        /// 添加角色权限表
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddRolePer(PermissionRoleRequstDtoTwo entity)
        {
            var slist = RepRolePermission.GetAll().Result.Where(x => x.RoleId == entity.RoleId).ToList();
            if (slist.Count() != 0)
            {
                await RepRolePermission.RealDel(slist);
            }

            List<RolePermission> rolePermission = new List<RolePermission>();
            foreach (var item in entity.PerIdAndButtonValue)
            {
                rolePermission.Add(new RolePermission { RoleId = entity.RoleId, PermissionId = item.PerId, ButtonValue = item.ButtonValue });
            }
            int n = await RepRolePermission.AddRange(rolePermission);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }

        }
        /// <summary>
        /// 角色显示功能
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<RoleShowResponseDto>>> GetRole()
        {
            var slist = await (await RepRole.GetAll()).Where(x => x.IsDel == true).ToListAsync();

            return new ResultResponseDto<List<RoleShowResponseDto>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<List<RoleShowResponseDto>>(slist)
            };
        }
        /// <summary>
        /// 根据Id获取角色信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<RoleShowResponseDto>> GetRoleById(int Id)
        {
            var slist = await RepRole.Get(Id);

            return new ResultResponseDto<RoleShowResponseDto>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<RoleShowResponseDto>(slist)
            };

        }
        /// <summary>
        /// 反填权限
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<RolePermissionResponseShowDto>>> GetRolePermissionByRoleId(int RoleId)
        {
            return new ResultResponseDto<List<RolePermissionResponseShowDto>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = mapper.Map<List<RolePermissionResponseShowDto>>(RepRolePermission.GetAll().Result.Where(x => x.RoleId == RoleId).ToList())
            };
        }
        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> UpdateRole(RoleRequstDto entity)
        {
            var slist = await (await RepRole.GetAll()).FirstOrDefaultAsync();

            var obj = mapper.Map<Role>(entity);
            int n = await RepRole.Update(obj);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }
        /// <summary>
        /// 添加自定义字段
        /// </summary>
        /// <param name="extend"></param>
        /// <returns></returns>
        public async Task<ResultResponseDto<int>> AddExtend(Extend extend)
        {
            int n = await Extend.Add(extend);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }
        /// <summary>
        /// 显示要使用的单元属性
        /// </summary>
        /// <returns></returns>
        public async Task<ResultResponseDto<List<Attributes>>> GetAttrbutes()
        {
            return new ResultResponseDto<List<Attributes>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = Attr.GetAll().Result.ToList()
            };
        }
        public async Task<ResultResponseDto<int>> Addattribute(Attributes attr)
        {
            int n = await Attr.Add(attr);
            if (n > 0)
            {
                return new ResultResponseDto<int>();
            }
            else
            {
                return new ResultResponseDto<int>(ResultCode.Fail, "添加失败");
            }
        }
        public async Task<ResultResponseDto<List<Extend>>> GetExtend()
        {
            return new ResultResponseDto<List<Extend>>()
            {
                Code = ResultCode.Ok,
                ErrorInfo = "获取成功",
                Data = Extend.GetAll().Result.ToList()
            };
        }
    }
}

